﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using IniFile;
using System.IO.Ports;
using NPOI.SS.Formula.Functions;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Reflection.Emit;
using System.Configuration;
using static Org.BouncyCastle.Math.EC.ECCurve;
using DevComponents.DotNetBar.Controls;
using System.Media;
using System.Drawing.Printing;

namespace Check_Barcode
{
    public partial class Setup : DevComponents.DotNetBar.Office2007Form
    {
       
        public Setup()
        {
            InitializeComponent();
        }

        private void Setup_Load(object sender, EventArgs e)
        {
            List<string> PrintsList = new List<string>();
            foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取计算机安装所有打印机的名称
            {
                PrintsList.Add(sPrint);
                comboBox_printer1.Items.Add(sPrint);
                comboBox_printer2.Items.Add(sPrint);
            }
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("可用的串口列表:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
                PortName.Items.Add(port);
            }
            //cmb_serialPort.SelectedItem = null;

            //comboBox_printer2.SelectedItem = PrintsList;
            ReadDataini();
           

        }

        #region 保存配置

        
        private void buttonX1_Click(object sender, EventArgs e)
        {
            // 创建配置文件对象
            // System.Configuration.ConfigurationSettings config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //修改配置节点AppSettings的内容
            config.AppSettings.Settings["softTile"].Value = title.Text;
            config.AppSettings.Settings["label_barcode1"].Value = title_sn1.Text;
            config.AppSettings.Settings["label_barcode2"].Value = title_sn2.Text;
            config.AppSettings.Settings["barcodeRule"].Value = barcode_rule.Text;
            config.AppSettings.Settings["cmb_Separator"].Value = cmb_Separator.SelectedIndex.ToString();
            config.AppSettings.Settings["handleError"].Value = handle_error.SelectedIndex.ToString();
            config.AppSettings.Settings["unlockPwd"].Value = unlock_pwd.Text;


            config.AppSettings.Settings["print1Flag"].Value = print1Flag.CheckValue.ToString();
            config.AppSettings.Settings["print2Flag"].Value = print2Flag.CheckValue.ToString();

            config.AppSettings.Settings["TemplatePath"].Value = t_TemplatePath.Text;
            config.AppSettings.Settings["TemplatePath2"].Value = t_TemplatePath2.Text;

            config.AppSettings.Settings["comboBox_printer1"].Value = comboBox_printer1.Text;
            config.AppSettings.Settings["comboBox_printer2"].Value = comboBox_printer2.Text;

            config.AppSettings.Settings["SoundPlayerOK"].Value = switchButton1.Value.ToString();
            config.AppSettings.Settings["SoundPlayerNG"].Value = switchButton2.Value.ToString();

            config.AppSettings.Settings["checkCuplicate"].Value = checkCuplicate.CheckValue.ToString();
            config.AppSettings.Settings["PortName"].Value = PortName.Text;

            config.AppSettings.Settings["cmb_printType"].Value = cmb_printType.Text;
            


            //保存配置文件
            config.Save();
          
            DialogResult dr = MessageBox.Show("保存成功,是否重新打开生效？", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                //this.Close();
                Application.ExitThread();
                //自重启代码
                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            }
            else
            {

            }

            this.Close();
        }
        #endregion


        #region 读取配置
        private void ReadDataini()
        {
            int skin;
            int handleError;
            int manualCodeType;          
            string TemplatePath;
            try
            {
                string ConString = ConfigurationManager.AppSettings["Connectionstring"];
                title.Text = ConfigurationManager.AppSettings["softTile"];
                title_sn1.Text = ConfigurationManager.AppSettings["label_barcode1"] + "";
                title_sn2.Text = ConfigurationManager.AppSettings["label_barcode2"] + "";

                barcode_rule.Text = ConfigurationManager.AppSettings["barcodeRule"] + "";
                manualCodeType = Convert.ToInt32(ConfigurationManager.AppSettings["manualCodeType"]);
                cmb_Separator.SelectedIndex = manualCodeType;

               

                skin = Convert.ToInt32(ConfigurationManager.AppSettings["skin"]);

                handleError = Convert.ToInt32(ConfigurationManager.AppSettings["handleError"]);
                handle_error.SelectedIndex = handleError;
                unlock_pwd.Text = ConfigurationManager.AppSettings["unlockPwd"];

                print1Flag.CheckValue = ConfigurationManager.AppSettings["print1Flag"];
                print2Flag.CheckValue = ConfigurationManager.AppSettings["print2Flag"];

                TemplatePath = ConfigurationManager.AppSettings["TemplatePath"] + "";
                t_TemplatePath.Text = TemplatePath;
                t_TemplatePath2.Text = ConfigurationManager.AppSettings["TemplatePath2"] + "";

                comboBox_printer1.Text = ConfigurationManager.AppSettings["comboBox_printer1"];
                comboBox_printer2.Text = ConfigurationManager.AppSettings["comboBox_printer2"];

                cmb_printType.SelectedItem = ConfigurationManager.AppSettings["cmb_printType"] + "";
                

                if (ConfigurationManager.AppSettings["SoundPlayerOK"] == "True")
                {
                    switchButton1.Value = true;
                }
                else
                {
                    switchButton1.Value = false;
                }
                if (ConfigurationManager.AppSettings["SoundPlayerNG"] == "True")
                {
                    switchButton2.Value = true;
                }
                else
                {
                    switchButton2.Value = false;
                }

                if (ConfigurationManager.AppSettings["checkCuplicate"] == "Y")
                {
                    checkCuplicate.CheckState= CheckState.Checked;
                }
                else
                {
                    checkCuplicate.CheckState = CheckState.Unchecked;
                }
                

                PortName.SelectedItem = ConfigurationManager.AppSettings["PortName"] + "";
            }
            catch (Exception)
            {

                throw;
            }            
        }
        #endregion

        private void CM_MODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupPanel1.Enabled = true;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Input_Excel frm = new Input_Excel();
            frm.Show();
        }

        private void t_TemplatePath_ButtonCustomClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog3 = new OpenFileDialog();
            //openFileDialog2.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//默认选择桌面文件
            openFileDialog3.Filter = "标签模板文件 (*.lab;*.btw)|*.lab;*.btw|所有文件 (*.*)|*.*";//文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*
            openFileDialog3.FilterIndex = 1; //默认选择第一个
            openFileDialog3.RestoreDirectory = true;
            openFileDialog3.Title = "选择标签模板, CodeSoft模板格式、BarTender模板格式";
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                t_TemplatePath.Text = openFileDialog3.FileName;
            }
            else
            {
                t_TemplatePath.Text = "";
            }
        }

        private void t_TemplatePath2_ButtonCustomClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog3 = new OpenFileDialog();
            //openFileDialog2.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//默认选择桌面文件
            openFileDialog3.Filter = "标签模板文件 (*.lab;*.btw)|*.lab;*.btw|所有文件 (*.*)|*.*";//文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*
            openFileDialog3.FilterIndex = 1; //默认选择第一个
            openFileDialog3.RestoreDirectory = true;
            openFileDialog3.Title = "选择标签模板, CodeSoft模板格式、BarTender模板格式";
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                t_TemplatePath2.Text = openFileDialog3.FileName;
            }
            else
            {
                t_TemplatePath2.Text = "";
            }
        }
    }
}
